﻿Public Class frmArticles
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing

    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Dim OCB_REF As String
    Dim szFamille As String

    Private Sub frmArticles_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        ' Init combobox FAMILLE
        Call InitComboTables(CB_FAMILLE, "P_FAMILLE")
        Call InitComboTables(CB_FAM, "P_FAMILLE")

        ' utilisateur autorisé à modifier ?
        If gUserAcces < 9 Then
            BO_ENREGISTRER.Enabled = False
        Else
            BO_ENREGISTRER.Enabled = True
        End If

    End Sub

    Private Sub BO_ENREGISTRER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTRER.Click

        If IsUserAuthorised(ctGestion) = False Then
            Exit Sub
        End If

        If TE_REF.TextLength = 0 Then
            MsgBox("Référence obligatoire")
            Exit Sub
        End If

        szSQL = "CALL spUPD_ARTICLE('" & gSocieteCode & "','" & TE_REF.Text & "','" & OCB_REF & "','" & szFamille & "','" & _
                 nq(TE_LIB.Text) & "','" & nq(TE_LIBCOMPL1.Text) & "','" & nq(TE_LIBCOMPL2.Text) & "','" & nq(TE_LIBCOMPL3.Text) & "','" & nq(TE_LIBCOMPL4.Text) & "','" & _
                 TE_UNITE.Text & "'," & TE_PRIXHT1.Value & "," & TE_PRIXHT2.Value & "," & TE_PRIXHT3.Value & "," & TE_PRIXHT4.Value & "," & _
                 TE_PRIXHT5.Value & "," & TE_PRIXHT6.Value & "," & TE_PRIXHT7.Value & "," & TE_PRIXHT8.Value & "," & TE_PRIXHT9.Value & "," & _
                 TE_PRIXHT10.Value & ")"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

    Private Sub BO_ANNULER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ANNULER.Click
        ' Cacher les détails
        TC_Details.Visible = False
        ' réaffiche la viewlist
        InitListeTarifs(LV_TARIFS.SelectedItems(0).Index)
    End Sub

    Private Sub InitListeTarifs(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_TARIFS.Items.Clear()
        LV_TARIFS.ListViewItemSorter = Nothing

        ' Lire la table FORMULES
        szSQL = "CALL spGet_ARTICLES_byFAM('" & gSocieteCode & "','" & szFamille & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)

        Do While Not rs.EOF
            LV_TARIFS.Items.Add(rs.Fields("ref").Value)
            LV_TARIFS.Items(nb).SubItems.Add(rs.Fields("libart").Value)
            LV_TARIFS.Items(nb).SubItems.Add(rs.Fields("prixht1").Value)
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " lignes"

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            m_SortingColumn.Text = m_SortingColumn.Text.Replace("> ", "")
            SortListView(LV_TARIFS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_TARIFS)
        ' Cacher les détails
        TC_Details.Visible = False

    End Sub

    Private Sub CB_FAMILLE_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles CB_FAMILLE.SelectedIndexChanged
        szFamille = getCodeFromCombo(CB_FAMILLE.SelectedItem)
        InitListeTarifs(0)
    End Sub

    Private Sub ArticletoScreen(ByVal ref As String)

        ' Lire l'enreg sélectionné
        szSQL = "CALL spGET_ARTICLES('" & gSocieteCode & "','" & ref & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_REF.Text = rs.Fields("ref").Value
            TE_LIB.Text = rs.Fields("libart").Value
            TE_LIBCOMPL1.Text = rs.Fields("libcompl1").Value
            TE_LIBCOMPL2.Text = rs.Fields("libcompl2").Value
            TE_LIBCOMPL3.Text = rs.Fields("libcompl3").Value
            TE_LIBCOMPL4.Text = rs.Fields("libcompl4").Value
            CB_FAM.SelectedIndex = CB_FAM.FindString(rs.Fields("fam").Value.ToString)
            TE_PRIXHT1.Value = rs.Fields("prixht1").Value
            TE_PRIXHT2.Value = rs.Fields("prixht2").Value
            TE_PRIXHT3.Value = rs.Fields("prixht3").Value
            TE_PRIXHT4.Value = rs.Fields("prixht4").Value
            TE_PRIXHT5.Value = rs.Fields("prixht5").Value
            TE_PRIXHT6.Value = rs.Fields("prixht6").Value
            TE_PRIXHT7.Value = rs.Fields("prixht7").Value
            TE_PRIXHT8.Value = rs.Fields("prixht8").Value
            TE_PRIXHT9.Value = rs.Fields("prixht9").Value
            TE_PRIXHT10.Value = rs.Fields("prixht10").Value
            ' TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
        End If
        rs.Close()

    End Sub

    Private Sub BO_DELETE_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_DELETE_LIGNE.Click

        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(ctCompta) = False Then
            Exit Sub
        End If

        deleteEnregistrement("ARTICLES", LV_TARIFS.SelectedItems(0).SubItems(1).Text, LV_TARIFS.SelectedItems(0).Text)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

    Private Sub LV_TARIFS_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_TARIFS.ColumnClick
        SortListView(LV_TARIFS, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_TARIFS_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_TARIFS.DoubleClick

        If LV_TARIFS.SelectedItems.Count > 0 Then
            ArticletoScreen(LV_TARIFS.SelectedItems(0).Text)
            ' afficher les détails de la formule
            TC_Details.Visible = True
            ' mémoriser ancien identifiant pour maj
            OCB_REF = TE_REF.Text
        End If

    End Sub

    Private Sub LV_TARIFS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_TARIFS.KeyDown

        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_TARIFS_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

            Case Keys.Delete
                BO_DELETE_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub BO_ADD_LIGNE_Click(sender As Object, e As System.EventArgs) Handles BO_ADD_LIGNE.Click

        ' utilisateur autorisé à ajouter ?
        If IsUserAuthorised(ctCompta) = False Then
            Exit Sub
        End If

        ' creation 
        ClearTextBoxes(Me)
        ' Ajouter un nouveau tarif
        OCB_REF = ""
        szSQL = "CALL spUPD_ARTICLE('" & gSocieteCode & "','NEW','" & OCB_REF & "','" & szFamille & _
            "','NOUVEAU','','','','','',0,0,0,0,0,0,0,0,0,0)"
        If ExecQuery(ctDSN_DEVFAC, szSQL) Then
            ' réaffiche la viewlist
            InitListeTarifs(0)
            ' ouvrir le fomulaire de modif
            LV_TARIFS_DoubleClick(sender, e)
        End If

    End Sub

End Class
